<?php

/**
 * This is the model class for table "site_administrator_menu".
 *
 * The followings are the available columns in table 'site_administrator_menu':
 * @property string $administrator_menu_id
 * @property string $administrator_menu_par_id
 * @property string $administrator_menu_title
 * @property string $administrator_menu_link
 * @property string $administrator_menu_icon
 * @property string $administrator_menu_order_by
 * @property string $administrator_menu_action_create
 * @property string $administrator_menu_action_update
 * @property string $administrator_menu_action_delete
 * @property string $administrator_menu_action_publish
 * @property string $administrator_menu_action_order_by
 * @property string $administrator_menu_action_activate
 * @property string $administrator_menu_action_process
 * @property string $administrator_menu_is_active
 *
 * The followings are the available model relations:
 * @property SiteAdministratorPrivilege[] $siteAdministratorPrivileges
 */
class site_administrator_menu extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return site_administrator_menu the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'site_administrator_menu';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('administrator_menu_par_id, administrator_menu_order_by', 'length', 'max' => 10),
            array('administrator_menu_title', 'length', 'max' => 50),
            array('administrator_menu_link, administrator_menu_icon', 'length', 'max' => 255),
            array('administrator_menu_action_create, administrator_menu_action_update, administrator_menu_action_delete, administrator_menu_action_publish, administrator_menu_action_order_by, administrator_menu_action_activate, administrator_menu_action_process, administrator_menu_is_active', 'length', 'max' => 1),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('administrator_menu_id, administrator_menu_par_id, administrator_menu_title, administrator_menu_link, administrator_menu_icon, administrator_menu_order_by, administrator_menu_action_create, administrator_menu_action_update, administrator_menu_action_delete, administrator_menu_action_publish, administrator_menu_action_order_by, administrator_menu_action_activate, administrator_menu_action_process, administrator_menu_is_active', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'rel_administrator_menu_to_administrator_privilege' => array(self::HAS_MANY, 'site_administrator_privilege', 'administrator_privilege_administrator_menu_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'administrator_menu_id' => 'Administrator Menu',
            'administrator_menu_par_id' => 'Menu Parent',
            'administrator_menu_title' => 'Menu Title',
            'administrator_menu_link' => 'Menu Link',
            'administrator_menu_icon' => 'Menu Icon',
            'administrator_menu_order_by' => 'Menu Order By',
            'administrator_menu_action_create' => 'Action Create',
            'administrator_menu_action_update' => 'Action Update',
            'administrator_menu_action_delete' => 'Action Delete',
            'administrator_menu_action_publish' => 'Action Publish',
            'administrator_menu_action_order_by' => 'Action Order By',
            'administrator_menu_action_activate' => 'Action Activate',
            'administrator_menu_action_process' => 'Action Process',
            'administrator_menu_is_active' => 'Is Active',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('administrator_menu_id', $this->administrator_menu_id, true);
        $criteria->compare('administrator_menu_par_id', $this->administrator_menu_par_id, true);
        $criteria->compare('administrator_menu_title', $this->administrator_menu_title, true);
        $criteria->compare('administrator_menu_link', $this->administrator_menu_link, true);
        $criteria->compare('administrator_menu_icon', $this->administrator_menu_icon, true);
        $criteria->compare('administrator_menu_order_by', $this->administrator_menu_order_by, true);
        $criteria->compare('administrator_menu_action_create', $this->administrator_menu_action_create, true);
        $criteria->compare('administrator_menu_action_update', $this->administrator_menu_action_update, true);
        $criteria->compare('administrator_menu_action_delete', $this->administrator_menu_action_delete, true);
        $criteria->compare('administrator_menu_action_publish', $this->administrator_menu_action_publish, true);
        $criteria->compare('administrator_menu_action_order_by', $this->administrator_menu_action_order_by, true);
        $criteria->compare('administrator_menu_action_activate', $this->administrator_menu_action_activate, true);
        $criteria->compare('administrator_menu_action_process', $this->administrator_menu_action_process, true);
        $criteria->compare('administrator_menu_is_active', $this->administrator_menu_is_active, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function listData($className=__CLASS__) {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('administrator_menu_par_id', '0', true);
        $criteria->order = "administrator_menu_order_by ASC";

        $par_menu = parent::model($className)->findAll($criteria);

        foreach ($par_menu as $data_par_menu) {

            //echo $data_par_menu->administrator_menu_title . "==>";

            $criteria = new CDbCriteria;

            $criteria->condition = 'administrator_menu_par_id = ' . $data_par_menu->administrator_menu_id;
            $criteria->order = "administrator_menu_order_by ASC";

            $child_menu_1 = parent::model($className)->findAll($criteria);
            $arr_child_menu_1[$data_par_menu->administrator_menu_id] = $child_menu_1;

            foreach ($child_menu_1 as $data_child_menu_1) {

                //echo $data_child_menu_1->administrator_menu_title . "===>";

                $criteria = new CDbCriteria;

                $criteria->condition = 'administrator_menu_par_id = ' . $data_child_menu_1->administrator_menu_id;
                $criteria->order = "administrator_menu_order_by ASC";

                $child_menu_2 = parent::model($className)->findAll($criteria);

                $arr_child_menu_2[$data_child_menu_1->administrator_menu_id] = $child_menu_2;
            }
        }

        return array("1" => $par_menu, "2" => $arr_child_menu_1, "3" => $arr_child_menu_2);
    }

    public function listParent($className=__CLASS__) {

        $criteria = new CDbCriteria;
        $criteria->compare('administrator_menu_par_id', '0', true);

        $par_menu = parent::model($className)->findAll($criteria);

        foreach ($par_menu as $data_par_menu) {

            $criteria = new CDbCriteria;
            $criteria->condition = 'administrator_menu_par_id = ' . $data_par_menu->administrator_menu_id;

            $child_menu_1 = parent::model($className)->findAll($criteria);
            $arr_child_menu_1[$data_par_menu->administrator_menu_id] = $child_menu_1;
        }

        return array("1" => $par_menu, "2" => $arr_child_menu_1);
    }

    public function parentMenu() {
        
        $data = self::listParent();
        $parent_menu = array();
        $parent_menu[0] = "ROOT";
        
        foreach ($data['1'] as $row) {
            $parent_menu[$row->administrator_menu_id] = $row->administrator_menu_title;
            foreach ($data['2'][$row->administrator_menu_id] as $row_2) {
                $parent_menu[$row_2->administrator_menu_id] = " -- " . $row_2->administrator_menu_title;
            }
        }
        
        return $parent_menu;
    }

}